package me.khrystal.rxnews.callback;

import android.graphics.Canvas;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;

/**
 * usage: RecyclerView拖拽滑动的回调
 * author: kHRYSTAL
 * create time: 16/4/8
 * update time:
 * email: 723526676@qq.com
 */
public class SimpleItemTouchHelperCallback extends ItemTouchHelper.Callback{

    private final OnMoveAndSwipeListener mAdapter;

    private boolean mIsLongPressDragEnabled = true;

    public SimpleItemTouchHelperCallback(OnMoveAndSwipeListener listener){
        mAdapter = listener;
    }

    public interface OnMoveAndSwipeListener{
        boolean onItemMove(int fromPosition,int toPosition);

        void onItemDismiss(int position);
    }

    public interface OnStartDragListener{
        void startDrag(RecyclerView.ViewHolder viewHolder);
    }

    public interface OnStateChangedListener{
        void onItemSelected();

        void onItemClear();
    }

    /**
     * 设置拖动的方向与侧滑的方向
     * @param recyclerView
     * @param viewHolder
     * @return
     */
    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {

        if (recyclerView.getLayoutManager() instanceof GridLayoutManager){
            //拖拽方式为上下左右
            final  int dragFlags = ItemTouchHelper.UP
                    |ItemTouchHelper.DOWN|ItemTouchHelper.LEFT|ItemTouchHelper.RIGHT;
            return makeMovementFlags(dragFlags,0);

        }else {
            final int dragFlags = ItemTouchHelper.UP|ItemTouchHelper.DOWN;
            final int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
            return makeMovementFlags(dragFlags,swipeFlags);
        }
    }


    public void setLongPressDragEnabled(boolean enable){
        mIsLongPressDragEnabled = enable;
    }

    public boolean isLongPressDragEnabled(){
        return mIsLongPressDragEnabled;
    }

    /**
     * 拖拽时回调此方法
     * @param recyclerView
     * @param viewHolder
     * @param target
     * @return
     */
    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        //拖拽交换的方法 如果itemType 不同 不允许交换位置
        if (viewHolder.getItemViewType() != target.getItemViewType()){
            return false;
        }

        return mAdapter.onItemMove(viewHolder.getAdapterPosition(),target.getAdapterPosition());
    }

    /**
     * 侧滑item回调此方法
     * @param viewHolder
     * @param direction
     */
    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        mAdapter.onItemDismiss(viewHolder.getAdapterPosition());
    }

    @Override
    public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
        //当前状态不是IDLE 说明正在拖拽或者侧滑
        if (actionState != ItemTouchHelper.ACTION_STATE_IDLE){
            if (viewHolder instanceof  OnStateChangedListener){
                OnStateChangedListener listener = (OnStateChangedListener)viewHolder;
                listener.onItemSelected();
            }
        }
        super.onSelectedChanged(viewHolder, actionState);
    }

    /**
     * 拖拽或侧滑完回调此方法 清除item上的一些状态
     * @param recyclerView
     * @param viewHolder
     */
    @Override
    public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        super.clearView(recyclerView, viewHolder);
        if (viewHolder instanceof  OnStateChangedListener){
            OnStateChangedListener listener = (OnStateChangedListener)viewHolder;
            //改变背景颜色
            listener.onItemClear();
        }
    }

    /**
     * 判断是拖拽还是侧滑
     * @param c
     * @param recyclerView
     * @param viewHolder
     * @param dX
     * @param dY
     * @param actionState
     * @param isCurrentlyActive
     */
    @Override
    public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
        if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
            final float alpha = 1 - Math.abs(dX) / (float) viewHolder.itemView.getWidth();
            viewHolder.itemView.setAlpha(alpha);
            viewHolder.itemView.setTranslationX(dX);
        }
    }
}
